<?php

namespace App\Http\Controllers;

use App\Models\Member;
use Illuminate\Http\Request;
use Kuyuan\Phpwidget\Str;

class MemberController extends Controller
{
    /**
     * 用户信息
     */
    public function index()
    {
        return response(['data'=>auth('member')->user()]);
    }

    //--------------------------------------------------------

    /**
     * 获取一个用户信息
     */
    public function get($_id)
    {
        return response(['data'=>Member::find($_id)]);
    }

    /**
     * 获取会员列表
     */
    public function getList(Request $request)
    {
        $_limit = $request->input('limit', [0,10]);
        $Member = Member::orderBy("id","desc")
            ->offset($_limit[0])
            ->limit($_limit[1])
            ->get();
        if($Member) {
            foreach($Member as $item) {
                $item->setAppends([
                    'red_number',
                    'format_balance',
                    'format_red_balance',
                    'register_date'
                ]);
            }
        }
        return response(["data"=>$Member]);
    }

    /**
     * 获取系统会员总数
     */
    public function total()
    {
        return response(["data"=>Member::count()]);
    }

    /**
     * 修改用户id
     * @param $_member_id
     * @param Request $request
     * @return mixed
     */
    public function edit($_member_id,Request $request)
    {
        $_balance = Str::format_money($request->balance,true);
        $Member = Member::find($_member_id);
        $Member->balance = $_balance;
        if($Member->save()) {
            $_response = response(["data"=>"success"]);
        } else {
            $_response = response(["message"=>"修改失败"]);
        }
        return $_response;
    }
}
